package ${package.Controller};

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.clientmobile.common.pojo.PageInfo;
import com.zmzncs.clientmobile.common.pojo.Rest;
import com.zmzncs.clientmobile.common.util.JsonUtil;
import com.zmzncs.clientmobile.common.util.RestUtil;
import org.springframework.beans.BeanUtils;
import ${package.Entity}.*;
import org.springframework.web.bind.annotation.*;
import ${package.Service}.${table.serviceName};
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import javax.validation.Valid;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@Api(tags = {"${table.comment!}"})
@RestController
<#else>
@Controller
</#if>@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>class ${table.controllerName}<#if superControllerClass??>:${superControllerClass}()</#if><#else><#if superControllerClass??>public class ${table.controllerName} extends ${superControllerClass}{<#else>public class ${table.controllerName} {</#if>

    @Autowired
<#--    private ${table.serviceName} ${(table.serviceName?substring(1))?uncap_first};-->
    private ${table.serviceName} ${(table.serviceName)?uncap_first};


    @ApiOperation(value = "新增")
    @PostMapping("add")
    public Rest add(@RequestBody @Valid ${entity}AddForm form){
        ${(table.serviceName)?uncap_first}.add(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    public Rest delete(@RequestParam Long id){
        ${(table.serviceName)?uncap_first}.delete(id);

        return RestUtil.success();
    }

    @ApiOperation(value = "更新")
    @PostMapping("update")
    public Rest update(@RequestBody @Valid ${entity}UpdateForm form){
        ${(table.serviceName)?uncap_first}.update(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Rest<PageInfo${r"<"}${entity}VO>> page(@Valid ${entity}SearchForm form){
        IPage<${entity}DTO> iPage = ${(table.serviceName)?uncap_first}.page(form);
        PageInfo<${entity}VO> pageInfo = JsonUtil.pageTransform(iPage, ${entity}VO.class);

        return RestUtil.success(pageInfo);
    }

    @ApiOperation(value = "详情")
    @GetMapping("get-by-id")
    public Rest<${entity}VO> getById(@RequestParam Long id){
        ${entity}DTO ${(entity)?uncap_first}DTO = ${(table.serviceName)?uncap_first}.getById(id);

        ${entity}VO ${(entity)?uncap_first}VO = new ${entity}VO();
        BeanUtils.copyProperties(${(entity)?uncap_first}DTO, ${(entity)?uncap_first}VO);

        return RestUtil.success(${(entity)?uncap_first}VO);
    }

}
</#if>